package com.ccx.coachsite.ui.own;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.SoftInfo;
import com.ccx.coachsite.util.CommonViewUtils;
import com.ccx.coachsite.webimage.ImageLoader;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class OwerCollectListAdapter extends OwerAdapterBase<SoftInfo>{
	
	//private List<boolean> lis
	private Map<Integer, Boolean> selectedMap;
	private boolean isEdit;
	
	public OwerCollectListAdapter(Context context, List<SoftInfo> list) {
		super(context, list);
		selectedMap = new HashMap<Integer, Boolean>();  
        for (int i = 0; i < list.size(); i++) {  
            selectedMap.put(i, false);  
            isEdit = false;
        }  
	}
	
	public void resetList(List<SoftInfo> list) {
		mList = null;
		mList = list;
		selectedMap.clear();
		for (int i = 0; i < list.size(); i++) {  
            selectedMap.put(i, false);  
        }  
		super.notifyDataSetChanged();
	}
	
	public Map<Integer, Boolean> getSelectedMap() {
		return selectedMap;
	}

	public void setSelectedMap(Map<Integer, Boolean> selectedMap) {
		this.selectedMap = selectedMap;
	}
	
	public void doSelectAll(boolean checked)
	{
		for (int i = 0; i < selectedMap.size(); i++) {  
            selectedMap.put(i, checked);  
        }  
		super.notifyDataSetChanged();
	}
	
	public boolean isEdit() {
		return isEdit;
	}

	public void setEdit(boolean isEdit) {
		this.isEdit = isEdit;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		boolean isCreate = false;
		if(convertView == null) {
			isCreate = true;
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
			if(holder.pos != position)
				isCreate = true;
		}
		if(isCreate)
		{
			convertView = mInflater.inflate(R.layout.ower_personal_collect_list_item, parent, false);
			convertView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ower_commom_item_selector));
			holder = new ViewHolder();
			holder.pos = position;
			holder.mIcon = (ImageView) convertView.findViewById(R.id.ower_collect_image);
			holder.nName = (TextView) convertView.findViewById(R.id.ower_collect_title);
			holder.mStar = (ImageView) convertView.findViewById(R.id.ower_collect_star);
			holder.mCheck = (CheckBox) convertView.findViewById(R.id.ower_app_check_item);
			convertView.setTag(holder);
		}
		/*
		if(convertView == null) {
			convertView = mInflater.inflate(R.layout.ower_personal_collect_list_item, parent, false);
			convertView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ower_commom_item_selector));
			holder = new ViewHolder();
			holder.mIcon = (ImageView) convertView.findViewById(R.id.ower_collect_image);
			holder.nName = (TextView) convertView.findViewById(R.id.ower_collect_title);
			holder.mStar = (ImageView) convertView.findViewById(R.id.ower_collect_star);
			holder.mCheck = (CheckBox) convertView.findViewById(R.id.ower_app_check_item);
			convertView.setTag(holder);
		}else 
		{
			holder = (ViewHolder) convertView.getTag();
		}
		*/
		String picturePath = mList.get(position).getIcon();
		
		if (picturePath != null && !picturePath.equals(holder.imageUrl)) 
		{
			holder.imageUrl = picturePath;
			holder.mIcon.setImageResource(R.drawable.ower_image_none);
			ImageLoader.loadBitmap(picturePath, holder.mIcon);
			holder.mIcon.setTag(picturePath);
		}
		CommonViewUtils.SetStarImage(mList.get(position).getStar(), holder.mStar);	
		holder.nName.setText(mList.get(position).getName());
		holder.mCheck.setChecked(selectedMap.get(position));
		holder.mCheck.setTag(position);
		holder.mCheck.setOnCheckedChangeListener(selectedCheckListener);
		
		if(isEdit)
			holder.mCheck.setVisibility(View.VISIBLE);
		else
			holder.mCheck.setVisibility(View.GONE);
		
		return convertView;
	}
	
	OnCheckedChangeListener selectedCheckListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			Integer pos = (Integer)buttonView.getTag();
			selectedMap.put(pos, isChecked);  
		}
	};
	
	class ViewHolder {
		ImageView mIcon;
		TextView nName;
		ImageView mStar;
		CheckBox mCheck;
		String imageUrl;
		int pos;
	}
}
